package com.tiyende.kash.loop.ui.activity

import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.common.CommonActivity
import com.tiyende.kash.loop.databinding.TiyendeActivityPickCityBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType
import com.tiyende.kash.loop.request.HttpHelper
import com.tiyende.kash.loop.ui.adapter.TiyendePickCityAdapter
import com.tiyende.kash.loop.utils.TiyendeAssetsJsonReader
import com.tiyende.kash.loop.utils.log.TiyendeLogger
import com.tiyende.kash.loop.weight.city.CityEntity
import com.tiyende.kash.loop.weight.city.EntityWrapper
import com.tiyende.kash.loop.weight.city.IndexableAdapter
import com.tiyende.kash.loop.weight.city.IndexableLayout

class TiyendePickCityActivity: CommonActivity() {
    lateinit var binding: TiyendeActivityPickCityBinding
    var pickCityAdapter: TiyendePickCityAdapter? = null

    override fun getTiyendeRootView(): View {
        binding = TiyendeActivityPickCityBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeTextTitleColor(Color.parseColor("#FFFFFF"))
        setTiyendeTextTitle(takeCityPageName())
        setTiyendeBackBtnRes(R.mipmap.tiyende_back_white)
        setTiyendeRightBtnRes(R.mipmap.tiyende_customer_white)
        setTiyendeSwitchLangBtnRes(R.mipmap.tiyende_language_icon)

        initTiyendeCityView()
    }

    override fun initTiyendeListener() {
        super.initTiyendeListener()
        // set Listener
        pickCityAdapter?.setOnItemContentClickListener(object : IndexableAdapter.OnItemContentClickListener<CityEntity> {
            override fun onItemClick(v: View?, originalPosition: Int, currentPosition: Int, entity: CityEntity) {
                val cityName = entity.name
                cityName?.let {
                    TiyendeLogger.e("cityName:$it  currentPosition:$currentPosition  originalPosition:$originalPosition")
                    showTiyendePickCityCallback(it)
                }
            }
        })
        pickCityAdapter?.setOnItemTitleClickListener(object : IndexableAdapter.OnItemTitleClickListener{
            override fun onItemClick(v: View?, currentPosition: Int, indexTitle: String?) {
                TiyendeLogger.e( "indexTitle:$indexTitle  currentPosition:$currentPosition")
            }
        })
    }

    override fun loadTiyendeData() {
        val cityList = ArrayList<CityEntity>()
        val dateList = TiyendeAssetsJsonReader.readCityListFromAssets()
        dateList.forEach {
            val cityEntity = CityEntity()
            cityEntity.name = it
            cityList.add(cityEntity)
        }
        pickCityAdapter?.setDatas(cityList,object : IndexableAdapter.IndexCallback<CityEntity>{
            override fun onFinished(datas: List<EntityWrapper<CityEntity>>) {
                // 数据处理完成后回调
            }
        })
        eventRemark = "${takeCityPageName()} Page"
    }

    /**
     * 初始化城市选择
     */
    private fun initTiyendeCityView(){
        binding.indexableLayout.setLayoutManager(LinearLayoutManager(this))
        // 快速排序。  排序规则设置为：只按首字母  （默认全拼音排序）  效率很高，是默认的10倍左右。  按需开启～
        binding.indexableLayout.setCompareMode(IndexableLayout.MODE_FAST);
        // setAdapter
        pickCityAdapter = TiyendePickCityAdapter(this)
        binding.indexableLayout.setAdapter(pickCityAdapter)
        // set Center OverlayView
        binding.indexableLayout.setOverlayStyle_Center()
    }

    /**
     * 获取页面名称
     */
    private fun takeCityPageName(): String{
        var showTitle = ""
        intent.extras?.let {
            showTitle = it.getString("pageName",getString(R.string.tiyende_choose_city))
        }
        return showTitle
    }

    /**
     * 选择城市返回
     */
    private fun showTiyendePickCityCallback(cityName: String) {
        val intent = intent
        intent.putExtra("pickCityName",cityName)
        setResult(RESULT_OK,intent)
        finish()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_PICK_CITY_START,eventRemark)
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.cancelTag(this)
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_PICK_CITY_END,eventRemark)
    }

}